
import { getSetting, chooseAddress, openSetting, showModal, showToast } from '../../utils/asyncWx.js'
import regeneratorRuntime from '../../lib/runtime/runtime'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: {},
    cartList: [],
    allChecked: [],
    totalPrice: 0,
    totalNum: 0
  },
  onShow() {
    const address = wx.getStorageSync("address")
    // 缓存中购物车数据
    const cartList = wx.getStorageSync("cart") || []
    // 判断是否为全选
    this.setCart(cartList)
    this.setData({
      address,
      cartList
    })
  },
  // 点击选择地址
  async handleChooseAddress() {
    try {
      // 获取权限状态
      const res1 = await getSetting()
      // 获取权限状态，当属性名很怪异的时候，都要用【】来获取属性值
      const scopeAddress = res1.authSetting["scope.address"]
      // 如果scope属性值为false，则先获取权限
      if (scopeAddress === false) {
        await openSetting()
      }
      let address = await chooseAddress()
      address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo
      // 存入本地中
      wx.setStorageSync("address", address);
    } catch (err) {
      console.log(err)
    }
  },
  // 监听checked改变
  handleCheckedChange(e) {
    const id = e.currentTarget.dataset.id
    let { cartList } = this.data
    let index = cartList.findIndex(v => v.goods_id === id)
    cartList[index].checked = !cartList[index].checked
    this.setData({
      cartList
    })
    this.setCart(cartList)
  },
  // 修改购物车数据
  setCart(cartList) {
    let totalNum = 0
    let totalPrice = 0
    let allChecked = true
    cartList.forEach(v => {
      if (v.checked) {
        totalNum += v.num
        totalPrice += v.goods_price * v.num
      } else {
        allChecked = false
      }
    })
    allChecked = cartList.length !== 0 ? allChecked : false
    wx.setStorageSync("cart", cartList)
    this.setData({
      allChecked,
      totalNum,
      totalPrice,
      cartList
    })
  },
  // 监听全选复选框的改变
  handleAllChenkedChange() {
    let { cartList, allChecked } = this.data
    allChecked = !allChecked
    cartList.forEach(v => v.checked = allChecked)
    this.setCart(cartList)
  },
  // 监听步进器
  async handleNumEdit(e) {
    const { operation, id } = e.currentTarget.dataset
    let { cartList } = this.data
    let index = cartList.findIndex(v => v.goods_id === id)
    // 判断是否删除
    if (cartList[index].num === 1 && operation === -1) {
      // 弹窗提示
      const res = await showModal({ content: "亲，您确定要删除吗？" })
      if (res.confirm) {
        cartList.splice(index, 1)
        this.setCart(cartList)
      }
    } else {
      cartList[index].num += operation
      this.setCart(cartList)
    }
  },
  // 点击跳转去商品界面
  toBuyGoods() {
    wx.switchTab({
      url: '/pages/catagory/catagory',
    })
  },
  // 监听结算进行支付
  async handlePay() {
    const { address, totalNum } = this.data
    if(!address.userName) {
      await showToast({title: '亲，您还没添加地址哦~'})
      return
    }
    if(totalNum === 0) {
      await showToast({title: '亲，您还没选择商品哦~'})
      return
    }
    wx.navigateTo({
      url: '/pages/pay/pay',
    })
  }
})